package com.mhg.socket;
/*
	需求 文件上传 把客户端的.txt文件 发送到服务端 在服务端将它存储起来
		  反馈给客户端一个 "成功"
*/
import java.net.*;
import java.io.*;
//客户端
class Demo_04_Tcp_Client 
{
	public static void main(String[] args) throws Exception
	{
		//1.创建Socket
		Socket socket = new Socket("127.0.0.1",10016);
		//2.读取文件
		BufferedReader br = new BufferedReader(new FileReader("one.txt"));
		//3.将文件发送给服务端
		OutputStream out = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(out,true);

		String line = null;
		while((line = br.readLine())!=null){
		
			pw.println(line);
		}
		//使用Socket类中的 shutdownOutput() 方法 结束输出
		socket.shutdownOutput();//关闭 客户端的输出流 相当于给流加了一个结束标记
		//4.接收服务端反馈
		BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		System.out.println(brIn.readLine());
	}
}
//服务端
class Demo_04_Tcp_Server
{
	public static void main(String[] args) throws Exception{
		//1.创建服务器端
		ServerSocket ss = new ServerSocket(10016);
		//2.获取连接
		Socket socket = ss.accept();
		//3.获取客户端发送过来的文件
		BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		//4.将文件存储到服务器指定位置
		PrintWriter out = new PrintWriter(new FileWriter("copy.txt"),true);

		String line = null;
		while((line = brIn.readLine())!=null){
			out.println(line);
		}

		//5.给客户端一个反馈 成功信息
		PrintWriter pwOut = new PrintWriter(socket.getOutputStream(),true);
		pwOut.println("成功");
	}
}